#!/usr/bin/python
# -*- coding: utf-8 -*-

import psycopg2
import sys
import json
import db_conf

con = None

try:
    config = db_conf.getConfig('metabvis.conf')
    con = psycopg2.connect(host=db_conf.getDBHost(config), database=db_conf.getDBName(config), port=db_conf.getDBPort(config), user=db_conf.getUser(config), password=db_conf.getPassword(config)) 
    cur = con.cursor()
    pa = {}
    
    print "Content-type: text/html;charset=utf-8\r\n"
    #metabobase_config (parameter varchar, value varchar)
    mconfig = {}
    cur.execute("select distinct parameter, value from metabobase_config")
    rows = cur.fetchall()
    for row in rows:
        pi = []
        for r in range(len(row)):
                pi.append(row[r])
        mconfig[row[0]] = row[1]
    pa["config"] = mconfig
    print json.dumps(pa)
except psycopg2.DatabaseError, e:
    print 'Error %s' % e    
    sys.exit(1)    
finally:
    if con:
        con.close()
